package com.sc.nft.admin.controller;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.net.URLDecoder;
import cn.hutool.crypto.digest.MD5;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.google.common.collect.Lists;
import com.mysql.cj.util.Base64Decoder;
import com.sc.nft.admin.request.*;
import com.sc.nft.annotation.SysMemberLogin;
import com.sc.nft.dao.AdminOperationLogDao;
import com.sc.nft.dao.MallOrderDao;
import com.sc.nft.entity.*;
import com.sc.nft.entity.vo.AdminInviteTaskVO;
import com.sc.nft.entity.vo.IntegralTaskVO;
import com.sc.nft.entity.vo.InviteTaskProductDetailVO;
import com.sc.nft.entity.vo.InviteTaskProductVO;
import com.sc.nft.enums.MessageTopic;
import com.sc.nft.service.*;
import com.sc.nft.sup.Result;
import com.sc.nft.wrapper.RequestWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;


@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/admin/supply")
@Api(tags="供应链管理模块")
public class SupplyApi {

    private final MallProductService mallProductService;
    private final MallOrderService mallOrderService;
    private final MallOrderDao mallOrderDao;
    private final AdminOperationLogDao adminOperationLogDao;
    private final String result="{\"code\":10000,\"message\":\"成功\"}";

    @PostMapping(value="/pushCommodityInfo", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "推送商品信息",notes="推送商品信息", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public String pushCommodityInfo(@RequestBody SmartSupplyRequest model){
        mallProductService.addSmartSupplyProduct(model.getData());
        return result;
    }

    @PostMapping(value="/sendNotify", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "发货通知接口",notes="发货通知接口", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public String sendNotify(@RequestBody SmartSupplyRequest model){
        JSONObject jsonObject = JSONObject.parseObject(Base64.decodeStr(model.getData()));
        MallOrder order = mallOrderDao.getByApiOrderNo(jsonObject.getString("orderSn"));
        mallOrderService.ship(order,jsonObject.getString("expressNo"),jsonObject.getString("expressName"));
        return result;
    }

    public static void main(String[] args) {
        System.out.println(Base64.decodeStr("eyJleHByZXNzTmFtZSI6IuS4remAmiIsIm9yZGVyU24iOiIxNjkzMjA3NDM1Nzg2NDg5MTgiLCJleHByZXNzTm8iOiI3NTM4MzcyOTE5NzM2MyIsIm9yZGVyRGV0YWlsSWRMaXN0IjpbNjE3MV0sIm11bHRpcGxlUGFja2FnZSI6W3siZXhwcmVzc05hbWUiOiLkuK3pgJoiLCJvcmRlclNuIjoiMTY5MzIwNzQzNTc4NjQ4OTE4IiwiZXhwcmVzc05vIjoiNzUzODM3MjkxOTczNjMiLCJvcmRlckRldGFpbEluZm8iOlt7InF1YW50aXR5IjoxLCJvcmRlckRldGFpbElkIjo2MTcxfV0sImV4cHJlc3NDb2RlIjoienRvIn1dLCJleHByZXNzQ29kZSI6Inp0byJ9"));
    }

    @PostMapping(value="/stockChangeNotify", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "库存变动通知接口",notes="发货通知接口", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public String stockChangeNotify(HttpServletRequest request){
        String requestData = URLDecoder.decode(new RequestWrapper(request).getBody(), Charset.defaultCharset());
        log.info("库存变动通知接口：{}",requestData);
        return result;
    }

}